import 'package:flutter/material.dart';
import 'package:flutter_axrjc/datasource/network/bean/SelectOption.dart';
import 'package:flutter_axrjc/style.dart';
import 'package:flutter_axrjc/widget/single_select_dialog/logic.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class SingleSelectDialog extends StatelessWidget {
  final String keywords;
  final List<SelectOption> list;
  final String title;
  final bool multi;
  final bool showCancelBtn;
  final bool required;
  final String messageTitle;

  const SingleSelectDialog(
      {required this.keywords,
      required this.list,
      required this.title,
      required this.messageTitle,
      required this.required,
      required this.multi,
      this.showCancelBtn = false,
      super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: SelectionController(keywords, list),
      assignId: true,
      builder: (logic) {
        return Center(
          child: Container(
            width: Get.width * 0.8,
            margin: const EdgeInsets.symmetric(horizontal: 48),
            decoration: const BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.all(Radius.circular(12)),
            ),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 12),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          title,
                          style: const TextStyle(
                              color: orangeColor,
                              fontSize: 17,
                              fontWeight: FontWeight.bold),
                        )
                      ],
                    ),
                  ),
                  Container(
                    margin:
                        const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
                    decoration: BoxDecoration(
                      color: const Color(0xFFF4F8FB),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: TextField(
                      controller: TextEditingController(text: logic.keywords),
                      style: const TextStyle(fontSize: 15),
                      decoration: const InputDecoration(
                        isCollapsed: true,
                        contentPadding:
                            EdgeInsets.symmetric(vertical: 8, horizontal: 8),
                        hintText: "请输入搜索内容",
                        border: InputBorder.none,
                      ),
                      onChanged: (value) {
                        logic.keywords = value;
                        logic.filterList();
                      },
                    ),
                  ),
                  Obx(() {
                    return Container(
                      color: Colors.transparent,
                      height: logic.height.value,
                      child: SingleChildScrollView(
                        child: ListView.builder(
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            key: logic.key,
                            itemCount: logic.showList.length,
                            itemBuilder: (c, i) {
                              if (multi) {
                                return _getMultiItem(logic.showList[i]);
                              } else {
                                return _getSigleItem(logic.showList[i]);
                              }
                            }),
                      ),
                    );
                  }),
                  Row(
                    children: [
                      showCancelBtn
                          ? Expanded(
                              child: FilledButton(
                                onPressed: () {
                                  Get.back();
                                },
                                style: FilledButton.styleFrom(
                                  minimumSize: const Size(double.infinity, 48),
                                  foregroundColor: orangeColor,
                                  backgroundColor: Colors.transparent,
                                  tapTargetSize:
                                      MaterialTapTargetSize.shrinkWrap,
                                  shape: const RoundedRectangleBorder(
                                      borderRadius: BorderRadius.only(
                                          bottomLeft: Radius.circular(12),
                                          bottomRight: Radius.circular(12))),
                                ),
                                child: const Text(
                                  "取消",
                                  style: TextStyle(color: Colors.grey),
                                ),
                              ),
                            )
                          : Container(),
                      Expanded(
                        child: FilledButton(
                          onPressed: () {
                            if (multi) {
                              if (required) {
                                if (logic.selectedList.isNotEmpty == true) {
                                  Get.back(result: logic.selectedList);
                                } else {
                                  if (messageTitle != "") {
                                    EasyLoading.showToast("请选择$messageTitle");
                                  } else {
                                    if (title != "请选择") {
                                      EasyLoading.showToast("请选择$title");
                                    } else {
                                      EasyLoading.showToast("请选择对应的选项");
                                    }
                                  }
                                }
                              } else {
                                Get.back(result: logic.selectedList);
                              }
                            } else {
                              if (required) {
                                if (messageTitle != "") {
                                  EasyLoading.showToast("请选择$messageTitle");
                                } else {
                                  if (title != "请选择") {
                                    EasyLoading.showToast("请选择$title");
                                  } else {
                                    EasyLoading.showToast("请选择对的应选项");
                                  }
                                }
                              } else {
                                Get.back(result: SelectOption("", ""));
                              }
                            }
                          },
                          style: FilledButton.styleFrom(
                            minimumSize: const Size(double.infinity, 48),
                            foregroundColor: orangeColor,
                            backgroundColor: Colors.transparent,
                            tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                            shape: const RoundedRectangleBorder(
                                borderRadius: BorderRadius.only(
                                    bottomLeft: Radius.circular(12),
                                    bottomRight: Radius.circular(12))),
                          ),
                          child: const Text("确定"),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget _getSigleItem(SelectOption option) {
    return Column(
      children: [
        InkWell(
          onTap: () {
            Get.back(result: option);
          },
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 12),
            constraints: const BoxConstraints(minHeight: 48),
            child: Row(
              children: [
                Expanded(
                    child: Text(
                  option.label,
                  style: const TextStyle(color: orangeColor, fontSize: 15),
                )),
                const Icon(
                  Icons.keyboard_arrow_right,
                  color: Colors.grey,
                )
              ],
            ),
          ),
        ),
      ],
    );
  }

  Widget _getMultiItem(SelectOption option) {
    return GetBuilder<SelectionController>(builder: (controller) {
      return Column(
        children: [
          Padding(
            padding: const EdgeInsets.symmetric(horizontal: 0),
            child: InkWell(
              onTap: () {
                bool res = controller.selectedList.contains(option);
                if (res) {
                  controller.selectedList.remove(option);
                } else {
                  controller.selectedList.add(option);
                }
                controller.update();
              },
              child: Row(
                children: [
                  IgnorePointer(
                    child: Checkbox(
                        value: controller.selectedList.contains(option),
                        onChanged: (value) {}),
                  ),
                  Expanded(
                      child: Text(
                    option.label,
                    style: const TextStyle(color: orangeColor, fontSize: 15),
                  )),
                  const Icon(
                    Icons.keyboard_arrow_right,
                    color: Colors.grey,
                  )
                ],
              ),
            ),
          ),
        ],
      );
    });
  }
}
